package base.Chapter00_Structures.C01_Array;

import org.testng.annotations.Test;

/**
 * 方法1:暴力
 * 从第一个开始遍历,求n项和
 */
public class L862_和至少为K的最短子数组 {
    public int shortestSubarray(int[] nums, int k) {
        if (nums == null || nums.length < 1) {
            return -1;
        }
        if (nums.length == 1)
            if (nums[0] >= k)
                return 1;

        //外层循环控制: 循环次数
        int result = -1;
        for (int i = 0; i < nums.length; i++) {
            int sum = 0;
            for (int j = i; j < nums.length; j++) {
                sum += nums[j];
                if (sum >= k) {
                    int r1 = j - i + 1;
                    result = result==-1? r1 :Math.min(r1,result);
                    break;
                }
            }
        }
        return result;
    }

    @Test
    public void testMethod() {
        int[] nums1 = {77, 19, 35, 10, -14};
        int[] nums = {17, 85, 93, -45, -21};
        System.out.println(shortestSubarray(nums, 150));
    }
}
